#include "QApplication.h"

#include <QDebug>
#include <database/SQLiteUtils.h>

const QString create_table = "\
CREATE TABLE COMPANY(\
   ID INT PRIMARY KEY     NOT NULL,\
   NAME           TEXT    NOT NULL,\
   AGE            INT     NOT NULL,\
   ADDRESS        CHAR(50),\
   SALARY         REAL\
);\
";

void executeSqlFile(const QString& fileName, QSqlDatabase& db) {
    // 读取 SQL 文件内容
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qDebug() << "Failed to open SQL file:" << file.errorString();
        return;
    }

    // 读取文件内容并拆分为单独的 SQL 语句
    QTextStream in(&file);
    QString     sqlContent    = in.readAll();
    sqlContent                = sqlContent.replace('\n', ' ');
    QStringList sqlStatements = sqlContent.split(';', QString::SkipEmptyParts);

    // 执行每个 SQL 语句
    QSqlQuery query(db);
    for (const QString& statement : sqlStatements) {
        qDebug() << statement + ';';
        query.prepare(statement);
        if (!query.exec()) {
            qDebug() << "Failed to execute SQL statement:" << query.lastError().text();
        }
        else {
            qDebug() << "Success to execute SQL statement";
        }
    }

    // 关闭文件
    file.close();
}

bool createDatabase() {
    // 检查数据库文件是否存在
    QFile file("test.db");
    if (!file.exists()) {
        // 如果文件不存在，创建数据库
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("test.db");

        if (!db.open()) {
            qDebug() << "Failed to create database.";
            return false;
        }

        // 执行sql文件来初始化数据库
        executeSqlFile("sql/test.sql", db);

        // // 创建表
        // QSqlQuery sql_query(db);
        // sql_query.prepare(create_table);
        // if (!sql_query.exec())
        //     qDebug() << "Error: Fail to create table." << sql_query.lastError();
        // else
        //     qDebug() << "Table created!";

        // 关闭数据库连接
        db.close();
    }

    return true;
}

void test() {
    auto db_path = "test.db";

    createDatabase();

    QSqlDatabase db;

    if (QSqlDatabase::contains(db_path)) {
        db = QSqlDatabase::database(db_path);
    }
    else {
        db = QSqlDatabase::addDatabase("QSQLITE", db_path);
        db.setDatabaseName(db_path);
    }
    if (!db.open()) {
        qDebug() << "Database connection failed.";
        return;
    }

    db.close();
}

int main(int argc, char* argv[]) {
    QApplication a(argc, argv);

    SQLiteUtils utils;

    LoginAccount account;
    account.userid().c_str()   = "100003";
    account.account  = "hansi";
    account.password = "hansi";
    account.headshot().c_str() = "D:/SGA/Pictures/head/r1.png";
    // account.userid().c_str()   = "100002";
    // account.account  = "fenglin";
    // account.password = "fenglin";
    // account.headshot().c_str() = "D:/SGA/Pictures/head/r2.png";
    utils.pushAccount(account);

    // _sleep(1);

    auto list = utils.getLoginAccounts();
    for (auto&& i : list) {
        qDebug() << i.userid().c_str() << "\t" << i.account << "\t" << i.headshot().c_str() << "\t" << i.time;
    }

    return a.exec();
}
